<?php

namespace App\Http\Controllers\Common;

use App\Models\Uploader;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UploadController extends Controller
{
    //文件上传的方法
    public function make(Request $request, Uploader $uploader)
    {
        //定义上传文件存放的路径
        $dir = 'upload/' . date('ym');
        $fileName = auth('admin')->id() . '-' . str_random(6) . time() . '.' . $request->file->getClientOriginalExtension();
        $file = $request->file->move($dir, $fileName);
        $file = str_replace('\\', '/', url($file));
        //上传完成后将上传的信息写入数据库中
//        这个上传是给后台管理人员使用的,用户要走auth('admin')这个认证
//        auth() 函数返回一个认证实例。为了方便起见，你可以使用它来替代 Auth Facade
        $uploader['admin_id'] = auth('admin')->id();
        $uploader['path'] = $file;
        $uploader['url'] = $file;
        $uploader['name'] = $request->file->getClientOriginalName();
        $uploader->save();
        //下面这个return是hdjs上传文件要求的返回
        return ['file' => $file, 'code' => 0];
    }

    function lists()
    {
        //        显示上传的图片
        $upload = Uploader::latest()->paginate(8);
//        dd($upload->toArray());
        return [
            'data' => $upload->toArray()['data'],
            'page' => $upload->links() . '',
            'code' => 0,
        ];
    }
}
